home *** CD-ROM | disk | FTP | other *** search
- Path: news2.cais.com!news
- From: Nehal Trivedi <ntrivedi@cais.com>
- Newsgroups: comp.lang.c++
- Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
- Date: Tue, 26 Mar 1996 23:53:55 -0500
- Organization: Instant Karma
- Message-ID: <3158C9E3.35B8@cais.com>
- References: <1996Mar14.155641.4299@schbbs.mot.com> <4irn11$7ln@mimas.brunel.ac.uk> <Pine.Sola.3.91.960322041345.17711C-100000@ux5.cso.uiuc.edu> <slrn4lerdj.2be.srkumar@shell.monmouth.com>
- NNTP-Posting-Host: ntrivedi.cais.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Senthil Kumar wrote:
- >
- > The way I look at exception handling (so far) is as a means of passing
- > error messages (which cannot be ignored) from one layer of code to a
- > higher layer of code, when possible errors cannot be resolved within
- > the layer that does the throw.
- >
- > For eg. consider a function called fprintf, which takes a FILE *
- > as its first argument. Now obviously, fprintf cannot do a damn thing
- > with a NULL FILE *, and thus in this case it would be a good idea to
- > throw an exception, telling the caller, "look, you better check this out."
- >
- > Am I on the right track? I would appreciate some comments, additional
- > refinements etc.
- >
- > thanks,
- > Senthil
-
- Think of exception handling as the way to handle errors. The advantage of using
- exceptions instead of return codes is that using exceptions consistently and
- properly allows for a clean distinction between normal logic (in the try block)
- and the error-detection and handling logic (in the catch block). Also since
- exceptions automatically propagate if no handled, each layer only has to define
- catch blocks for the error-conditions that it knows how to recover from. Also
- each layer can make a decision about what diagnostic action to take in response
- to an error e.g. log the error to the system log.
- Typically "harsh" decisions such as program termination should be handled by the
- outermost catch block.
-
- In my experience the best way to handle exceptions is to use a small but
- well-defined hierarchy of exception classes that cover all the error types in
- your system.
-
- Nehal Trivedi
-